package com.caffeinatedbliss.nanowrimo.test;

import com.caffeinatedbliss.nanowrimo.NanowrimoComputer;
import junit.framework.TestCase;

public class NanowrimoComputerUT extends TestCase {

  public NanowrimoComputerUT(String name) {
    super(name);
  }

  public void testNullAndEmptyStringDontCauseException() throws Exception {
    try {
      new NanowrimoComputer((String) null, 50000);
      new NanowrimoComputer("", 50000);
    } catch (Exception e) {
      fail("Null/Empty shouldnt kill the constructor");
    }
  }

  public void testArraysCreatedCorrectSizes() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("1 2 3", 50000);
    assertEquals(3, computer.getHistoryCounts().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getPace().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getExpectedPerDayAmount().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getExpectedWordCount().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getBoundries().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getPaceExpected().length);
    assertEquals(NanowrimoComputer.DAYS_IN_NANO, computer.getOutliers().length);
  }

  public void testHistoryCorrectlyParsed() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("100 200 300", 50000);
    assertEquals(600, computer.getCurrent());
    int[] historyCounts = computer.getHistoryCounts();
    int[] expectedCounts = {100, 200, 300};
    assertEquals(expectedCounts.length, historyCounts.length);
    for (int i = 0; i < expectedCounts.length; i++) {
      assertEquals(expectedCounts[i], historyCounts[i]);
    }
  }

  public void testPace() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("100 200 300", 50000);
    int[] pace = computer.getPace();
    int[] expected = {100, 150, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (int i = 0; i < expected.length; i++) {
      assertEquals(expected[i], pace[i]);
    }
  }

  public void testExpectedPerDayAmount() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("1000 2000 3000", 50000);
    int[] perDay = computer.getExpectedPerDayAmount();
    int[] expected = {50000 / 30, 49000 / 29, 47000 / 28, 44000 / 27};
    for (int i = 0; i < expected.length; i++) {
      assertEquals("iteration " + i, expected[i], perDay[i]);
    }
  }

  public void testBoundaries() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("4500 4500 3000 8000 4500 4500 8000 3000", 50000);
    boolean[] perDay = computer.getBoundries();
    boolean[] expected = {false, false, true, true, false, false, true, true};
    for (int i = 0; i < expected.length; i++) {
      assertEquals("iteration " + i, expected[i], perDay[i]);
    }
  }

  public void testOutlierDetection() throws Exception {
    NanowrimoComputer computer = new NanowrimoComputer("4500 4500 5000 9000 2900 109", 50000);
    boolean[] outliers = computer.getOutliers();
    boolean[] expected = {false, false, false, true, true, true};
    for (int i = 0; i < expected.length; i++) {
      assertEquals("outlier at index " + i, expected[i], outliers[i]);
    }
  }
}
